<!DOCTYPE html>
<html lang="">

<head>
	<meta name="generator" content="Hugo 0.73.0" />
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<meta name="author" content="Ap Chen ">
<meta name="description" content="Windows 简介 Windows: 基于图形界面的多任务操作系统 多任务：同时运行多个应用程序 窗口：应用程序通过窗口与用户交互 输入：鼠标单击/双击/拖放，菜单选择，键盘输" />
<meta name="keywords" content="blog" />
<meta name="robots" content="noodp" />

<link rel="canonical" href="https://fziks.gitee.io/notes/advanced-programming/7-windows/" />

<meta itemprop="name" content="Windows">
<meta itemprop="description" content="Windows 简介 Windows: 基于图形界面的多任务操作系统 多任务：同时运行多个应用程序 窗口：应用程序通过窗口与用户交互 输入：鼠标单击/双击/拖放，菜单选择，键盘输">
<meta itemprop="datePublished" content="2019-02-13T00:00:00&#43;00:00" />
<meta itemprop="dateModified" content="2019-02-13T00:00:00&#43;00:00" />
<meta itemprop="wordCount" content="3500">



<meta itemprop="keywords" content="" />
<meta property="og:title" content="Windows" />
<meta property="og:description" content="Windows 简介 Windows: 基于图形界面的多任务操作系统 多任务：同时运行多个应用程序 窗口：应用程序通过窗口与用户交互 输入：鼠标单击/双击/拖放，菜单选择，键盘输" />
<meta property="og:type" content="article" />
<meta property="og:url" content="https://fziks.gitee.io/notes/advanced-programming/7-windows/" />
<meta property="article:published_time" content="2019-02-13T00:00:00+00:00" />
<meta property="article:modified_time" content="2019-02-13T00:00:00+00:00" />

<meta name="twitter:card" content="summary"/>
<meta name="twitter:title" content="Windows"/>
<meta name="twitter:description" content="Windows 简介 Windows: 基于图形界面的多任务操作系统 多任务：同时运行多个应用程序 窗口：应用程序通过窗口与用户交互 输入：鼠标单击/双击/拖放，菜单选择，键盘输"/>


<link rel="apple-touch-icon" sizes="60x60" href="https://fziks.gitee.io/icons/apple-touch-icon.png">
<link rel="icon" type="image/png" sizes="32x32" href="https://fziks.gitee.io/icons/favicon-32x32.png">
<link rel="icon" type="image/png" sizes="16x16" href="https://fziks.gitee.io/icons/favicon-16x16.png">
<link rel="manifest" href="https://fziks.gitee.io/icons/site.webmanifest">
<link rel="mask-icon" href="https://fziks.gitee.io/icons/safari-pinned-tab.svg" color="#5bbad5">
<link rel="shortcut icon" href="https://fziks.gitee.io/icons/favicon.ico">
<meta name="msapplication-TileColor" content="#ffffff">
<meta name="msapplication-config" content="/icons/browserconfig.xml">
<meta name="theme-color" content="#ffffff">

<title>Windows</title>


<link rel="stylesheet" href="//at.alicdn.com/t/font_1559566_wk214kwa2dn.css">


    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/katex@0.11.1/dist/katex.min.css" integrity="sha384-zB1R0rpPzHqg7Kpt0Aljp8JPLqbXI3bhnPWROx27a9N0Ll6ZP/+DiW/UqRcLbRjq" crossorigin="anonymous">



    
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/normalize/8.0.1/normalize.css" integrity="sha256-WAgYcAck1C1/zEl5sBl5cfyhxtLgKGdpI3oKyJffVRI=" crossorigin="anonymous" />
    
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/animate.css/3.7.2/animate.css" integrity="sha256-a2tobsqlbgLsWs7ZVUGgP5IvWZsx8bTNQpzsqCSm5mk=" crossorigin="anonymous" />
    
   <link href="https://stackpath.bootstrapcdn.com/bootswatch/4.4.1/materia/bootstrap.min.css" rel="stylesheet" integrity="sha384-1tymk6x9Y5K+OF0tlmG2fDRcn67QGzBkiM3IgtJ3VrtGrIi5ryhHjKjeeS60f1FA" crossorigin="anonymous">
    
    
    <link rel="stylesheet" href="https://fziks.gitee.io/sass/main_cdn.min.270b43bb8631af4497ed45b90db42c517e86c9511418de9152f134d02ed32b87.min.2192baea245cf318085511589e62bfbdb3fbe4fb0eef718f1be9af91c10542ce.css" integity="sha256-IZK66iRc8xgIVRFYnmK/vbP75PsO73GPG&#43;mvkcEFQs4=">

</head>

<body style="overflow-x: unset;">
	<div class="container-fluid">
		<div class="row d-print-block">
			<div class="col-12 col-md-3 col-lg-2 bd-sidebar d-print-none">
				<div class="d-flex mt-3 border-bottom">
        <span class="navbar-brand w-100" style="display: grid;">
            <small>
                <a href="https://fziks.gitee.io/" class="text-black-50">
                    <i class="iconfont icon-back-arrow-"></i>
                </a>
                Ap Chen's
            </small>
            <a class="text-dark" href="https://fziks.gitee.io/notes/">
                Notes
            </a>
        </span>
        <button class="btn btn-link text-dark d-md-none p-0 ml-3" type="button" data-toggle="collapse"
            data-target="#bd-docs-nav" aria-controls="bd-docs-nav" aria-expanded="true"
            aria-label="Toggle docs navigation">
            <i class="fad fa-bars"></i>
        </button>
    </div>
				<nav id="bd-docs-nav" class="collapse bd-links">
    
    
    
    <div class="bd-toc-item">
        <a class="bd-toc-link" href="https://fziks.gitee.io/notes/computer-network/">
            
            
                <span class="icontext">S</span>
            
            
            计算机网络
        </a>
    </div>
    
    
    
    <div class="bd-toc-item">
        <a class="bd-toc-link" href="https://fziks.gitee.io/notes/operating-system/">
            
            
                <span class="icontext">S</span>
            
            
            操作系统
        </a>
    </div>
    
    
    
    <div class="bd-toc-item">
        <a class="bd-toc-link" href="https://fziks.gitee.io/notes/database/">
            
            
                <span class="icontext">S</span>
            
            
            数据库
        </a>
    </div>
    
    
    
    <div class="bd-toc-item">
        <a class="bd-toc-link" href="https://fziks.gitee.io/notes/cs50-introduction-to-computer-science/">
            
            
                <span class="icontext">S</span>
            
            
            CS50 MIT
        </a>
    </div>
    
    
    
    <div class="bd-toc-item active bg-light">
        <a class="bd-toc-link" href="https://fziks.gitee.io/notes/advanced-programming/">
            <i class="iconfont icon-back-arrow-reverse"></i>
            高级程序设计
        </a>
        <ul class="nav bd-sidenav">
            
            
            <li class="active">
                <a href="https://fziks.gitee.io/notes/advanced-programming/7-windows/">
                    <i class="fad fa-chevron-right mr-1"></i>
                    Windows
                </a>
            </li>
            
            
            
            <li>
                <a href="https://fziks.gitee.io/notes/advanced-programming/8-qt/">Qt</a>
            </li>
            
            
            
            <li>
                <a href="https://fziks.gitee.io/notes/advanced-programming/java/">Java</a>
            </li>
            
            
        </ul>
    </div>
    
    
    
    <div class="bd-toc-item">
        <a class="bd-toc-link" href="https://fziks.gitee.io/notes/deep-learning/">
            
            
                <span class="icontext">O</span>
            
            
            深度学习基础
        </a>
    </div>
    
    
    
    <div class="bd-toc-item">
        <a class="bd-toc-link" href="https://fziks.gitee.io/notes/%E6%96%87%E7%8C%AE%E7%AE%A1%E7%90%86%E4%B8%8E%E4%BF%A1%E6%81%AF%E5%88%86%E6%9E%90/">
            
            
                <span class="icontext">O</span>
            
            
            文献管理与信息分析
        </a>
    </div>
    
    
</nav>
			</div>
			<div class="col-12 col-md-9 col-lg-10 d-print-block">
				<div class="row d-print-block">
					<main class="col-12 col-md-10 col-lg-9 py-md-3 pl-md-5 bd-content d-print-block" role="main">
						<div id="title" class="my-4 border-bottom">
							<span>高级程序设计</span>
							<h2>Windows</h2>
							<footer>
								<span>
									<i class="iconfont icon-NewFile mr-2"></i>
									2019-02-13 08:00 CST
								</span> <br />
								<span>
									<i class="iconfont icon-modify mr-2"></i>
									2019-02-13 08:00 CST
								</span> <br />
								<span>
									<i class="iconfont icon-copyright mr-2"></i>
									CC BY-NC 4.0
								</span>
							</footer>
						</div>
						<div id="content" class="hl-h2">
							
							
							
							
							
							
							
							
							
							
							
							
							<h2 id="windows-简介">Windows 简介<a href="#windows-简介" class="anchor" aria-hidden="true"><i class="iconfont icon-link"></i></a></h2>
<ul>
<li>Windows: 基于图形界面的多任务操作系统
<ul>
<li>多任务：同时运行多个应用程序</li>
<li>窗口：应用程序通过窗口与用户交互</li>
<li>输入：鼠标单击/双击/拖放，菜单选择，键盘输入</li>
</ul>
</li>
<li>Windows 功能
<ul>
<li>工具：资源管理器，记事本，画图 ...</li>
<li>函数库：作为 Windows 应用程序接口（API）提供 <code>windows.h</code></li>
</ul>
</li>
<li>应用程序类型
<ul>
<li>单文档应用
<ul>
<li>只能对一个文档数据操纵</li>
<li>必须首先结束当前文档的所有操作之后，才能进行下一个文档的操作</li>
</ul>
</li>
<li>多文档应用
<ul>
<li>同时可以对多个文档的数据进行操作</li>
<li>不必等到一个文档的所有操作结束，就可以对其它文档进行操作，对不同文档的操作是在不同的子窗口中进行的</li>
</ul>
</li>
<li>对话框应用
<ul>
<li>以对话框的形式操作一个文档数据</li>
<li>对文档数据的操作以各种“控件”（control）来实现</li>
<li>程序以按&lt;确定&gt;或&lt;取消&gt;等按钮来结束</li>
</ul>
</li>
</ul>
</li>
</ul>
<h2 id="消息驱动程序结构">消息驱动程序结构<a href="#消息驱动程序结构" class="anchor" aria-hidden="true"><i class="iconfont icon-link"></i></a></h2>
<ul>
<li>Windows 应用程序的结果属于消息（事件）驱动的结构：程序的任何一个动作都是在接收到一条消息后发生的</li>
</ul>
<table>
<thead>
<tr>
<th>Windows 消息</th>
<th></th>
</tr>
</thead>
<tbody>
<tr>
<td>WM_KEYDOWN/WM_KEYUP</td>
<td>键盘按键</td>
</tr>
<tr>
<td>WM_CHAR</td>
<td>字符</td>
</tr>
<tr>
<td>WM_LBUTTONDOWN/WM_LBUTTONUP/WM_LBUTTONDBLCLK/WM_MOUSEMOVE</td>
<td>鼠标按键</td>
</tr>
<tr>
<td>WM_COMMAND</td>
<td>菜单</td>
</tr>
<tr>
<td>WM_PAINT</td>
<td>窗口内容刷新</td>
</tr>
<tr>
<td>WM_TIMER</td>
<td>定时器消息</td>
</tr>
</tbody>
</table>
<ul>
<li>消息队列：Windows 系统会把属于各个应用程序的消息放入各自的消息队列
<ul>
<li>大部分消息关联到某个窗口</li>
<li>每个窗口有一个消息处理函数</li>
</ul>
</li>
<li>消息循环：取消息-处理消息
<ul>
<li>取到 WM_QUIT，循环结束</li>
</ul>
</li>
<li>可再入：函数的调用者要自带工作区（数据空间）
<ul>
<li>由于消息处理函数在处理一条消息时可能会主动产生一些新消息，这些消息并不放入消息队列，而是直接调用消息处理函数来处理这些消息，这样就有可能导致消息处理函数的一次执行还未结束，另一次执行就开始的现象，这可能会引起数据的不一致错误。</li>
<li>函数需要的数据要通过参数来传递</li>
<li>函数不能有 static 存储类的局部变量</li>
<li>在函数中访问全局变量也可能导致函数不可再入</li>
</ul>
</li>
</ul>
<h2 id="windows-api">Windows API<a href="#windows-api" class="anchor" aria-hidden="true"><i class="iconfont icon-link"></i></a></h2>
<ul>
<li>基于 Windows API 的过程式 Windows 应用程序设计
<ul>
<li>是一种基于过程抽象的程序设计范式</li>
<li>通过调用 API 函数编写程序的粒度太细、太繁琐，开发效率不高</li>
</ul>
</li>
<li>主函数 WinMain：每个 Windows 应用程序都必须提供
<ul>
<li>注册窗口类（窗口基本信息）
<ul>
<li>窗口类的名字、窗口的基本风格、消息处理函数名、图标、光标、背景颜色以及菜单</li>
<li>每类窗口（不是每个窗口）都需要注册</li>
</ul>
</li>
<li>创建应用程序主窗口</li>
<li>进入消息循环</li>
<li>为每类窗口提供一个消息处理函数，用于处理发送到它的消息</li>
</ul>
</li>
</ul>
<div class="highlight"><pre style="background-color:#f8f8f8;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-cpp" data-lang="cpp"><span style="color:#8f5902;font-style:italic">#include</span> <span style="color:#8f5902;font-style:italic">&lt;windows.h&gt;</span><span style="color:#8f5902;font-style:italic">
</span><span style="color:#8f5902;font-style:italic"></span><span style="color:#204a87;font-weight:bold">int</span> <span style="color:#000">APIENTRY</span> <span style="color:#000">WinMain</span><span style="color:#000;font-weight:bold">(</span><span style="color:#000">HINSTANCE</span> <span style="color:#000">hInstance</span><span style="color:#000;font-weight:bold">,</span> <span style="color:#8f5902;font-style:italic">//本实例标识，Handle
</span><span style="color:#8f5902;font-style:italic"></span>                     <span style="color:#000">HINSTANCE</span> <span style="color:#000">hPrevInstance</span><span style="color:#000;font-weight:bold">,</span> <span style="color:#8f5902;font-style:italic">//上一实例标识
</span><span style="color:#8f5902;font-style:italic"></span>                     <span style="color:#000">LPSTR</span> <span style="color:#000">lpCmdLine</span><span style="color:#000;font-weight:bold">,</span> <span style="color:#8f5902;font-style:italic">//命令行参数
</span><span style="color:#8f5902;font-style:italic"></span>                     <span style="color:#204a87;font-weight:bold">int</span> <span style="color:#000">nCmdShow</span><span style="color:#000;font-weight:bold">)</span> <span style="color:#8f5902;font-style:italic">//主窗口显示方式
</span><span style="color:#8f5902;font-style:italic"></span><span style="color:#000;font-weight:bold">{</span>
    <span style="color:#8f5902;font-style:italic">//注册窗口类
</span><span style="color:#8f5902;font-style:italic"></span>    <span style="color:#000">RegisterClass</span><span style="color:#000;font-weight:bold">(...,</span> <span style="color:#000">WindowProc</span><span style="color:#000;font-weight:bold">,</span> <span style="color:#4e9a06">&#34;my\_window\_class&#34;</span><span style="color:#000;font-weight:bold">);</span>
    <span style="color:#8f5902;font-style:italic">//创建并显示主窗口
</span><span style="color:#8f5902;font-style:italic"></span>    <span style="color:#000">HWND</span> <span style="color:#000">hWnd</span><span style="color:#000;font-weight:bold">;</span>
    <span style="color:#000">hWnd</span> <span style="color:#ce5c00;font-weight:bold">=</span> <span style="color:#000">CreateWindow</span><span style="color:#000;font-weight:bold">(</span><span style="color:#4e9a06">&#34;my\_window\_class&#34;</span><span style="color:#000;font-weight:bold">,</span> <span style="color:#000;font-weight:bold">...,</span> <span style="color:#000">x</span><span style="color:#000;font-weight:bold">,</span> <span style="color:#000">y</span><span style="color:#000;font-weight:bold">,</span> <span style="color:#000">width</span><span style="color:#000;font-weight:bold">,</span> <span style="color:#000">height</span><span style="color:#000;font-weight:bold">,</span> <span style="color:#000;font-weight:bold">...);</span>
    <span style="color:#000">ShowWindow</span><span style="color:#000;font-weight:bold">(</span><span style="color:#000">hWnd</span><span style="color:#000;font-weight:bold">,</span> <span style="color:#000">nCmdShow</span><span style="color:#000;font-weight:bold">);</span>
    <span style="color:#8f5902;font-style:italic">//消息循环
</span><span style="color:#8f5902;font-style:italic"></span>    <span style="color:#204a87;font-weight:bold">while</span> <span style="color:#000;font-weight:bold">(</span><span style="color:#000">GetMessage</span><span style="color:#000;font-weight:bold">(</span><span style="color:#ce5c00;font-weight:bold">&amp;</span><span style="color:#000">msg</span><span style="color:#000;font-weight:bold">,</span> <span style="color:#204a87">NULL</span><span style="color:#000;font-weight:bold">,</span> <span style="color:#0000cf;font-weight:bold">0</span><span style="color:#000;font-weight:bold">,</span> <span style="color:#0000cf;font-weight:bold">0</span><span style="color:#000;font-weight:bold">)){</span><span style="color:#8f5902;font-style:italic">//从消息队列中取消息
</span><span style="color:#8f5902;font-style:italic"></span>        <span style="color:#000">DispatchMessage</span><span style="color:#000;font-weight:bold">(</span><span style="color:#ce5c00;font-weight:bold">&amp;</span><span style="color:#000">msg</span><span style="color:#000;font-weight:bold">);</span>
    <span style="color:#000;font-weight:bold">}</span>
    <span style="color:#204a87;font-weight:bold">return</span> <span style="color:#000">msg</span><span style="color:#000;font-weight:bold">.</span><span style="color:#000">wParam</span><span style="color:#000;font-weight:bold">;</span>
<span style="color:#000;font-weight:bold">}</span>
<span style="color:#8f5902;font-style:italic">//窗口消息处理函数
</span><span style="color:#8f5902;font-style:italic"></span><span style="color:#000">LRESULT</span> <span style="color:#000">CALLBACK</span> <span style="color:#000">WindowProc</span><span style="color:#000;font-weight:bold">(</span><span style="color:#000">HWND</span> <span style="color:#000">hWnd</span><span style="color:#000;font-weight:bold">,</span> <span style="color:#8f5902;font-style:italic">//窗口标识
</span><span style="color:#8f5902;font-style:italic"></span>                            <span style="color:#000">UINT</span> <span style="color:#000">message</span><span style="color:#000;font-weight:bold">,</span> <span style="color:#8f5902;font-style:italic">//消息标识
</span><span style="color:#8f5902;font-style:italic"></span>                            <span style="color:#000">WPARAM</span> <span style="color:#000">wParam</span><span style="color:#000;font-weight:bold">,</span> <span style="color:#8f5902;font-style:italic">//消息的参数1
</span><span style="color:#8f5902;font-style:italic"></span>                            <span style="color:#000">LPARAM</span> <span style="color:#000">lParam</span><span style="color:#000;font-weight:bold">)</span> <span style="color:#8f5902;font-style:italic">//消息的参数2
</span><span style="color:#8f5902;font-style:italic"></span><span style="color:#000;font-weight:bold">{</span>
    <span style="color:#204a87;font-weight:bold">switch</span> <span style="color:#000;font-weight:bold">(</span><span style="color:#000">message</span><span style="color:#000;font-weight:bold">){</span>
        <span style="color:#204a87;font-weight:bold">case</span> <span style="color:#000">WM</span><span style="color:#a40000">\</span><span style="color:#f57900">_KEYDOWN</span><span style="color:#000;font-weight:bold">:</span>
            <span style="color:#8f5902;font-style:italic">//wParam 为扫描码
</span><span style="color:#8f5902;font-style:italic"></span>        <span style="color:#204a87;font-weight:bold">case</span> <span style="color:#000">WM</span><span style="color:#a40000">\</span><span style="color:#f57900">_CHAR</span><span style="color:#000;font-weight:bold">:</span> <span style="color:#8f5902;font-style:italic">//字符键消息
</span><span style="color:#8f5902;font-style:italic"></span>            <span style="color:#8f5902;font-style:italic">//wParam 为按键字符编码
</span><span style="color:#8f5902;font-style:italic"></span>        <span style="color:#204a87;font-weight:bold">case</span> <span style="color:#000">WM</span><span style="color:#a40000">\</span><span style="color:#f57900">_COMMAND</span><span style="color:#000;font-weight:bold">:</span> <span style="color:#8f5902;font-style:italic">//菜单消息
</span><span style="color:#8f5902;font-style:italic"></span>            <span style="color:#204a87;font-weight:bold">switch</span> <span style="color:#000;font-weight:bold">(</span><span style="color:#000">wParam</span><span style="color:#000;font-weight:bold">)</span> <span style="color:#000;font-weight:bold">{</span><span style="color:#8f5902;font-style:italic">//wParam 菜单项标识
</span><span style="color:#8f5902;font-style:italic"></span>                <span style="color:#204a87;font-weight:bold">case</span> <span style="color:#000">ID</span><span style="color:#a40000">\</span><span style="color:#000">_FILE</span><span style="color:#a40000">\</span><span style="color:#f57900">_OPEN</span><span style="color:#000;font-weight:bold">:</span>
                <span style="color:#204a87;font-weight:bold">case</span> <span style="color:#000">ID</span><span style="color:#a40000">\</span><span style="color:#000">_START</span><span style="color:#a40000">\</span><span style="color:#f57900">_TIMER</span><span style="color:#000;font-weight:bold">:</span>
                    <span style="color:#000">SetItimer</span><span style="color:#000;font-weight:bold">(</span><span style="color:#000">hWnd</span><span style="color:#000;font-weight:bold">,</span> <span style="color:#0000cf;font-weight:bold">1</span><span style="color:#000;font-weight:bold">,</span> <span style="color:#0000cf;font-weight:bold">1000</span><span style="color:#000;font-weight:bold">,</span> <span style="color:#204a87">NULL</span><span style="color:#000;font-weight:bold">);</span>
            <span style="color:#000;font-weight:bold">}</span>
        <span style="color:#204a87;font-weight:bold">case</span> <span style="color:#000">WM</span><span style="color:#a40000">\</span><span style="color:#f57900">_LBUTTONDOWN</span><span style="color:#000;font-weight:bold">:</span>
            <span style="color:#8f5902;font-style:italic">//lParam 鼠标在窗口中位置
</span><span style="color:#8f5902;font-style:italic"></span>        <span style="color:#204a87;font-weight:bold">case</span> <span style="color:#000">WM</span><span style="color:#a40000">\</span><span style="color:#f57900">_CLOSE</span><span style="color:#000;font-weight:bold">:</span>
            <span style="color:#000">DestroyWindow</span><span style="color:#000;font-weight:bold">(</span><span style="color:#000">hWnd</span><span style="color:#000;font-weight:bold">);</span><span style="color:#8f5902;font-style:italic">//关闭窗口
</span><span style="color:#8f5902;font-style:italic"></span>            <span style="color:#204a87;font-weight:bold">break</span><span style="color:#000;font-weight:bold">;</span>
        <span style="color:#204a87;font-weight:bold">case</span> <span style="color:#000">WM</span><span style="color:#a40000">\</span><span style="color:#f57900">_DESTORY</span><span style="color:#000;font-weight:bold">:</span> <span style="color:#8f5902;font-style:italic">//窗口被关闭
</span><span style="color:#8f5902;font-style:italic"></span>            <span style="color:#000">PostQuitMessage</span><span style="color:#000;font-weight:bold">(</span><span style="color:#0000cf;font-weight:bold">0</span><span style="color:#000;font-weight:bold">);</span> <span style="color:#8f5902;font-style:italic">//往本应用消息队列中放入 WM\_QUIT
</span><span style="color:#8f5902;font-style:italic"></span>            <span style="color:#204a87;font-weight:bold">break</span><span style="color:#000;font-weight:bold">;</span>
        <span style="color:#204a87;font-weight:bold">default</span><span style="color:#ce5c00;font-weight:bold">:</span> <span style="color:#8f5902;font-style:italic">//由系统做默认的消息处理
</span><span style="color:#8f5902;font-style:italic"></span>            <span style="color:#204a87;font-weight:bold">return</span> <span style="color:#000">DefWindownProc</span><span style="color:#000;font-weight:bold">(</span><span style="color:#000">hWnd</span><span style="color:#000;font-weight:bold">,</span> <span style="color:#000">message</span><span style="color:#000;font-weight:bold">,</span> <span style="color:#000">wParam</span><span style="color:#000;font-weight:bold">,</span> <span style="color:#000">lParam</span><span style="color:#000;font-weight:bold">);</span>
    <span style="color:#000;font-weight:bold">}</span>
    <span style="color:#204a87;font-weight:bold">return</span> <span style="color:#0000cf;font-weight:bold">0</span><span style="color:#000;font-weight:bold">;</span>
<span style="color:#000;font-weight:bold">}</span>
</code></pre></div><ul>
<li>消息处理函数中生成新消息
<ul>
<li>PostMessage: 放入消息队列 <code>PostMessage(HWND hwnd, UINT Msg, WPARAM wParam, LPARAM lParam)</code></li>
<li>SendMessage: 直接调用消息处理函数</li>
</ul>
</li>
<li>Resource
<ul>
<li>菜单：菜单 ID、菜单项 ID/显示文字</li>
<li>对话框：对话框 ID 和尺寸；对话框中各个控件的 ID、类型、尺寸与位置</li>
</ul>
</li>
<li>资源文件(.rc)：编译后作为应用程序一部分链接到目标文件中
<ul>
<li>资源描述有规定的格式</li>
<li>可以用 VC++的资源编辑器来进行可视化编辑</li>
</ul>
</li>
</ul>
<h2 id="mfc">MFC<a href="#mfc" class="anchor" aria-hidden="true"><i class="iconfont icon-link"></i></a></h2>
<ul>
<li>基于 MFC 和“文档--视”结构的面向对象 Windows 应用程序设计
<ul>
<li>MFC(Microsoft Foundation Class library, 微软基础类库)</li>
<li>MFC 提供了一些类来描述应用中对象的基本功能，应用程序可以通过继承这些类来实现各自的特殊功能</li>
<li>文档--视结构
<ul>
<li>文档：储存和管理程序中的数据</li>
<li>视：显示文档数据以及实现对文档数据进行操作时与用户的交互功能</li>
<li>一个文档对象对应一个或多个视对象</li>
</ul>
</li>
</ul>
</li>
<li>MFC 提供的文档视框架主要由以下类构成
<ul>
<li>CView</li>
<li>CDocument</li>
<li>CFrameWnd</li>
<li>CDocTemplate</li>
<li>CWinApp</li>
</ul>
</li>
<li>类向导 Class Wizard
<ul>
<li>为应用程序中从 MFC 派生的类增加/删除成员</li>
<li>为应用程序增加/删除基于 MFC 的类</li>
</ul>
</li>
<li>图形用户接口(GUI)设计
<ul>
<li>菜单：菜单由菜单项构成，每个菜单项包含标识，显示文字和提示文字，处理函数</li>
<li>对话框</li>
<li>绘图</li>
<li>坐标转换</li>
</ul>
</li>
</ul>
<h3 id="窗口类">窗口类<a href="#窗口类" class="anchor" aria-hidden="true"><i class="iconfont icon-link"></i></a></h3>
<ul>
<li>
<p>基本窗口类 CWnd</p>
<ul>
<li>实现窗口的基本功能：一般的消息处理、窗口大小和位置管理、菜单管理、坐标系管理、滚动条管理、剪贴板管理、窗口状态管理、窗口间位置关系管理，等等</li>
<li>其它窗口类的基类</li>
</ul>
</li>
<li>
<p>框架窗口类</p>
<ul>
<li>提供对标题栏、菜单栏、工具栏、状态栏以及属于它的子窗口的管理功能</li>
<li>CFrameWnd: 单文档应用主窗口</li>
<li>CMDIFramWnd: 多文档应用主窗口</li>
<li>CMDIChildWnd: 多文档应用子窗口</li>
</ul>
</li>
<li>
<p>视类 CView</p>
<ul>
<li>视窗口（简称视）位于单文档应用主窗口（CFrameWnd）和多文档应用子窗口（CMDIChildWnd）的客户区（可显示区）</li>
<li>实现程序数据的显示功能以及操作数据时与用户的交互功能</li>
<li>CScrollView 带滚动功能</li>
</ul>
<div class="highlight"><pre style="background-color:#f8f8f8;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-cpp" data-lang="cpp"><span style="color:#204a87;font-weight:bold">void</span> <span style="color:#000">CScrollView</span><span style="color:#ce5c00;font-weight:bold">::</span><span style="color:#000">SetScrollSizes</span><span style="color:#000;font-weight:bold">(</span>
  <span style="color:#204a87;font-weight:bold">int</span> <span style="color:#000">nMapMode</span><span style="color:#000;font-weight:bold">,</span> <span style="color:#8f5902;font-style:italic">//映射方式（坐标的逻辑单位，坐标轴方向）
</span><span style="color:#8f5902;font-style:italic"></span>  <span style="color:#000">SIZE</span> <span style="color:#000">sizeTotal</span><span style="color:#000;font-weight:bold">,</span> <span style="color:#8f5902;font-style:italic">//文档内容大小
</span><span style="color:#8f5902;font-style:italic"></span>  <span style="color:#204a87;font-weight:bold">const</span> <span style="color:#000">SIZE</span><span style="color:#ce5c00;font-weight:bold">&amp;</span> <span style="color:#000">sizePage</span> <span style="color:#ce5c00;font-weight:bold">=</span> <span style="color:#000">sizeDefault</span><span style="color:#000;font-weight:bold">,</span> <span style="color:#8f5902;font-style:italic">//文档滚动页大小
</span><span style="color:#8f5902;font-style:italic"></span>  <span style="color:#204a87;font-weight:bold">const</span> <span style="color:#000">SIZE</span><span style="color:#ce5c00;font-weight:bold">&amp;</span> <span style="color:#000">sizeLine</span> <span style="color:#ce5c00;font-weight:bold">=</span> <span style="color:#000">sizeDefault</span> <span style="color:#8f5902;font-style:italic">//文档滚动行大小
</span><span style="color:#8f5902;font-style:italic"></span><span style="color:#000;font-weight:bold">);</span>
<span style="color:#8f5902;font-style:italic">//坐标转换函数
</span><span style="color:#8f5902;font-style:italic"></span><span style="color:#000">OnPrepareDC</span><span style="color:#000;font-weight:bold">(</span><span style="color:#ce5c00;font-weight:bold">&amp;</span><span style="color:#000">dc</span><span style="color:#000;font-weight:bold">,</span> <span style="color:#204a87">NULL</span><span style="color:#000;font-weight:bold">);</span> <span style="color:#8f5902;font-style:italic">//为ScrollView初始化dc
</span><span style="color:#8f5902;font-style:italic"></span><span style="color:#000">dc</span><span style="color:#000;font-weight:bold">.</span><span style="color:#000">DPtoLP</span><span style="color:#000;font-weight:bold">(</span><span style="color:#ce5c00;font-weight:bold">&amp;</span><span style="color:#000">point</span><span style="color:#000;font-weight:bold">);</span> <span style="color:#8f5902;font-style:italic">//从物理坐标转换逻辑坐标
</span><span style="color:#8f5902;font-style:italic"></span><span style="color:#000">dc</span><span style="color:#000;font-weight:bold">.</span><span style="color:#000">LPtoDP</span><span style="color:#000;font-weight:bold">(</span><span style="color:#ce5c00;font-weight:bold">&amp;</span><span style="color:#000">point</span><span style="color:#000;font-weight:bold">);</span>
</code></pre></div><ul>
<li>CEditView 具有编辑功能</li>
<li>CFormView 具有表格功能</li>
<li>CHtmlView 具有Web浏览功能</li>
</ul>
</li>
</ul>
<div class="highlight"><pre style="background-color:#f8f8f8;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-cpp" data-lang="cpp"><span style="color:#8f5902;font-style:italic">//CView 成员
</span><span style="color:#8f5902;font-style:italic"></span><span style="color:#000">CDocument</span> <span style="color:#ce5c00;font-weight:bold">*</span><span style="color:#000">m</span><span style="color:#a40000">\</span><span style="color:#000">_pDocument</span><span style="color:#000;font-weight:bold">;</span> <span style="color:#8f5902;font-style:italic">//储存相应文档对象指针
</span><span style="color:#8f5902;font-style:italic"></span><span style="color:#000">CDocument</span> <span style="color:#ce5c00;font-weight:bold">*</span><span style="color:#000">GetDocument</span><span style="color:#000;font-weight:bold">()</span> <span style="color:#204a87;font-weight:bold">const</span><span style="color:#000;font-weight:bold">;</span>
<span style="color:#204a87;font-weight:bold">virtual</span> <span style="color:#204a87;font-weight:bold">void</span> <span style="color:#000">OnDraw</span><span style="color:#000;font-weight:bold">(</span><span style="color:#000">CDC</span><span style="color:#ce5c00;font-weight:bold">*</span> <span style="color:#000">pDC</span><span style="color:#000;font-weight:bold">)</span><span style="color:#ce5c00;font-weight:bold">=</span><span style="color:#0000cf;font-weight:bold">0</span><span style="color:#000;font-weight:bold">;</span> <span style="color:#8f5902;font-style:italic">//处理窗口（视）刷新信息
</span><span style="color:#8f5902;font-style:italic"></span><span style="color:#204a87;font-weight:bold">virtual</span> <span style="color:#204a87;font-weight:bold">void</span> <span style="color:#000">OnInitialUpdate</span><span style="color:#000;font-weight:bold">();</span> <span style="color:#8f5902;font-style:italic">//视对象创建时被调用
</span><span style="color:#8f5902;font-style:italic"></span><span style="color:#204a87;font-weight:bold">virtual</span> <span style="color:#204a87;font-weight:bold">void</span> <span style="color:#000">OnUpdate</span><span style="color:#000;font-weight:bold">(</span><span style="color:#000">CView</span><span style="color:#ce5c00;font-weight:bold">*</span> <span style="color:#000">pSender</span><span style="color:#000;font-weight:bold">,</span> <span style="color:#000">LPARAM</span> <span style="color:#000">lHint</span><span style="color:#000;font-weight:bold">,</span> <span style="color:#000">CObject</span><span style="color:#ce5c00;font-weight:bold">*</span> <span style="color:#000">pHint</span><span style="color:#000;font-weight:bold">);</span> <span style="color:#8f5902;font-style:italic">//文档对象的数据发生改变时调用该函数刷新相应的视对象。默认处理：发送WM\_PAINT消息
</span></code></pre></div><ul>
<li>对话框类 CDialog
<ul>
<li>对话框是一种特殊的窗口，用于获取用户的输入信息</li>
<li>CFileDialog: 文件打开/保存对话框</li>
<li>CFontDialog: 字体选择对话框</li>
<li>CColorDialog: 颜色选择对话框</li>
<li>CPrintDialog: 打印对话框</li>
<li>CFindReplaceDialog: 查找/替换对话框</li>
</ul>
</li>
</ul>
<div class="highlight"><pre style="background-color:#f8f8f8;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-cpp" data-lang="cpp"><span style="color:#000">CMyDlg</span> <span style="color:#000">dlg</span><span style="color:#000;font-weight:bold">;</span>
<span style="color:#000">dlg</span><span style="color:#000;font-weight:bold">.</span><span style="color:#000">m</span><span style="color:#a40000">\</span><span style="color:#000">_</span> <span style="color:#ce5c00;font-weight:bold">=</span> <span style="color:#000;font-weight:bold">...;</span>
<span style="color:#204a87;font-weight:bold">if</span> <span style="color:#000;font-weight:bold">(</span><span style="color:#000">dlg</span><span style="color:#000;font-weight:bold">.</span><span style="color:#000">DoModal</span><span style="color:#000;font-weight:bold">()</span><span style="color:#ce5c00;font-weight:bold">==</span><span style="color:#000">IDOK</span><span style="color:#000;font-weight:bold">){</span>
  <span style="color:#000;font-weight:bold">...</span> <span style="color:#ce5c00;font-weight:bold">=</span> <span style="color:#000">dlg</span><span style="color:#000;font-weight:bold">.</span><span style="color:#000">m</span><span style="color:#a40000">\</span><span style="color:#000">_</span><span style="color:#000;font-weight:bold">;</span>
<span style="color:#000;font-weight:bold">}</span>
</code></pre></div><h3 id="文档类">文档类<a href="#文档类" class="anchor" aria-hidden="true"><i class="iconfont icon-link"></i></a></h3>
<ul>
<li>
<p>CDocument</p>
<ul>
<li>对程序要处理的数据进行管理，包括磁盘文件 I/O。</li>
<li>一个 CDocument 类的对象至少要对应一个 CView 类的对象。</li>
</ul>
<div class="highlight"><pre style="background-color:#f8f8f8;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-cpp" data-lang="cpp"><span style="color:#8f5902;font-style:italic">//CDocument 成员
</span><span style="color:#8f5902;font-style:italic"></span><span style="color:#204a87;font-weight:bold">void</span> <span style="color:#000">AddView</span><span style="color:#000;font-weight:bold">(</span><span style="color:#000">CView</span><span style="color:#ce5c00;font-weight:bold">*</span> <span style="color:#000">pView</span><span style="color:#000;font-weight:bold">);</span> <span style="color:#8f5902;font-style:italic">//给文档对象增加一个关联的CView类的对象
</span><span style="color:#8f5902;font-style:italic"></span><span style="color:#204a87;font-weight:bold">void</span> <span style="color:#000">RemoveView</span><span style="color:#000;font-weight:bold">(</span><span style="color:#000">CView</span><span style="color:#ce5c00;font-weight:bold">*</span> <span style="color:#000">pView</span><span style="color:#000;font-weight:bold">);</span> <span style="color:#8f5902;font-style:italic">//使一个CView对象脱离与文档对象的关联
</span><span style="color:#8f5902;font-style:italic"></span><span style="color:#204a87;font-weight:bold">virtual</span> <span style="color:#000">POSITION</span> <span style="color:#000">GetFirstViewPosition</span><span style="color:#000;font-weight:bold">()</span> <span style="color:#204a87;font-weight:bold">const</span><span style="color:#000;font-weight:bold">;</span> <span style="color:#8f5902;font-style:italic">//获取关联的第一个CView对象的位置
</span><span style="color:#8f5902;font-style:italic"></span><span style="color:#204a87;font-weight:bold">virtual</span> <span style="color:#000">CView</span><span style="color:#ce5c00;font-weight:bold">*</span> <span style="color:#000">GetNextView</span><span style="color:#000;font-weight:bold">(</span><span style="color:#000">POSITION</span><span style="color:#ce5c00;font-weight:bold">&amp;</span> <span style="color:#000">rPosition</span><span style="color:#000;font-weight:bold">)</span> <span style="color:#204a87;font-weight:bold">const</span><span style="color:#000;font-weight:bold">;</span> <span style="color:#8f5902;font-style:italic">//获取指定位置的CView对象，rPosition自动往后移动一个位置
</span><span style="color:#8f5902;font-style:italic"></span><span style="color:#204a87;font-weight:bold">void</span> <span style="color:#000">UpdateAllViews</span><span style="color:#000;font-weight:bold">(</span><span style="color:#000">CView</span><span style="color:#ce5c00;font-weight:bold">*</span> <span style="color:#000">pSender</span><span style="color:#000;font-weight:bold">,</span> <span style="color:#000">LPARAM</span> <span style="color:#000">lHint</span><span style="color:#ce5c00;font-weight:bold">=</span><span style="color:#0000cf;font-weight:bold">0L</span><span style="color:#000;font-weight:bold">,</span> <span style="color:#000">CObject</span><span style="color:#ce5c00;font-weight:bold">*</span> <span style="color:#000">pHint</span><span style="color:#ce5c00;font-weight:bold">=</span><span style="color:#204a87">NULL</span><span style="color:#000;font-weight:bold">);</span> <span style="color:#8f5902;font-style:italic">//向关联的CView对象发送刷新消息。当pSender为NULL时，向关联的所有CView对象发送刷新消息
</span><span style="color:#8f5902;font-style:italic"></span><span style="color:#204a87;font-weight:bold">void</span> <span style="color:#000">SetModifiedFlag</span><span style="color:#000;font-weight:bold">(</span><span style="color:#000">BOOL</span> <span style="color:#000">bModified</span><span style="color:#ce5c00;font-weight:bold">=</span><span style="color:#000">TRUE</span><span style="color:#000;font-weight:bold">);</span> <span style="color:#8f5902;font-style:italic">//设置文档修改标记
</span><span style="color:#8f5902;font-style:italic"></span><span style="color:#000">BOOL</span> <span style="color:#000">IsModified</span><span style="color:#000;font-weight:bold">();</span> <span style="color:#8f5902;font-style:italic">//判断文档是否被修改
</span><span style="color:#8f5902;font-style:italic"></span><span style="color:#204a87;font-weight:bold">virtual</span> <span style="color:#000">BOOL</span> <span style="color:#000">OnSaveDocument</span><span style="color:#000;font-weight:bold">(</span><span style="color:#000">LPCTSTR</span> <span style="color:#000">lpszPathName</span><span style="color:#000;font-weight:bold">);</span> <span style="color:#8f5902;font-style:italic">//把文档数据保存到文件名为 lpszPathName 的文件中去
</span><span style="color:#8f5902;font-style:italic"></span><span style="color:#204a87;font-weight:bold">virtual</span> <span style="color:#000">BOOL</span> <span style="color:#000">OnOpenDocument</span><span style="color:#000;font-weight:bold">(</span><span style="color:#000">LPCTSTR</span> <span style="color:#000">lpszPathName</span><span style="color:#000;font-weight:bold">);</span>
<span style="color:#204a87;font-weight:bold">virtual</span> <span style="color:#000">BOOL</span> <span style="color:#000">OnNewDocument</span><span style="color:#000;font-weight:bold">();</span>
<span style="color:#204a87;font-weight:bold">virtual</span> <span style="color:#000">BOOL</span> <span style="color:#000">OnCloseDocument</span><span style="color:#000;font-weight:bold">();</span>
<span style="color:#204a87;font-weight:bold">virtual</span> <span style="color:#204a87;font-weight:bold">void</span> <span style="color:#000">Serialize</span><span style="color:#000;font-weight:bold">(</span><span style="color:#000">CArchive</span><span style="color:#ce5c00;font-weight:bold">&amp;</span> <span style="color:#000">ar</span><span style="color:#000;font-weight:bold">);</span> <span style="color:#8f5902;font-style:italic">//用于文档数据的序列化（写入磁盘文件或从磁盘文件读出），一般由OnSaveDocument和OnOpenDocument来调用
</span></code></pre></div></li>
</ul>
<h3 id="应用程序类">应用程序类<a href="#应用程序类" class="anchor" aria-hidden="true"><i class="iconfont icon-link"></i></a></h3>
<ul>
<li>
<p>文档模板类</p>
<ul>
<li>实现对文档、视窗口和框架窗口所构成的对象组的管理功能。（用于支持基于“文档－视”结构的应用框架）</li>
<li>CDocTemplate: 文档模板的基类</li>
</ul>
<div class="highlight"><pre style="background-color:#f8f8f8;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-cpp" data-lang="cpp"><span style="color:#000">CDocTemplate</span><span style="color:#000;font-weight:bold">(</span><span style="color:#000">UINT</span> <span style="color:#000">nIDResource</span><span style="color:#000;font-weight:bold">,</span> <span style="color:#000">CRuntimeClass</span><span style="color:#ce5c00;font-weight:bold">*</span> <span style="color:#000">pDocClass</span><span style="color:#000;font-weight:bold">,</span> <span style="color:#000">CRuntimeClass</span><span style="color:#ce5c00;font-weight:bold">*</span> <span style="color:#000">pFrameClass</span><span style="color:#000;font-weight:bold">,</span> <span style="color:#000">CRuntimeClass</span><span style="color:#ce5c00;font-weight:bold">*</span> <span style="color:#000">pViewClass</span><span style="color:#000;font-weight:bold">);</span> <span style="color:#8f5902;font-style:italic">//参数为文档、视以及框架窗口的类信息
</span></code></pre></div><ul>
<li>CSingleDocTemplate: 单文档模板类</li>
<li>CMultiDocTemplate: 多文档模板类</li>
</ul>
</li>
<li>
<p>应用类 CWinApp</p>
<ul>
<li>提供了对 Windows 应用程序的各部分进行组合和管理的功能，其中包括对主窗口和文档模板的管理以及实现消息循环等</li>
</ul>
<div class="highlight"><pre style="background-color:#f8f8f8;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-cpp" data-lang="cpp"><span style="color:#204a87;font-weight:bold">virtual</span> <span style="color:#000">BOOL</span> <span style="color:#000">InitInstance</span><span style="color:#000;font-weight:bold">();</span> <span style="color:#8f5902;font-style:italic">//应用程序初始化，包括注册窗口类、创建/显示主窗口等。它由WinMain调用
</span><span style="color:#8f5902;font-style:italic"></span><span style="color:#204a87;font-weight:bold">virtual</span> <span style="color:#204a87;font-weight:bold">int</span> <span style="color:#000">Run</span><span style="color:#000;font-weight:bold">();</span> <span style="color:#8f5902;font-style:italic">//实现消息循环，由WinMain调用
</span><span style="color:#8f5902;font-style:italic"></span><span style="color:#204a87;font-weight:bold">virtual</span> <span style="color:#204a87;font-weight:bold">int</span> <span style="color:#000">ExitInstance</span><span style="color:#000;font-weight:bold">();</span> <span style="color:#8f5902;font-style:italic">//由Run调用
</span><span style="color:#8f5902;font-style:italic"></span><span style="color:#204a87;font-weight:bold">virtual</span> <span style="color:#000">CWnd</span> <span style="color:#ce5c00;font-weight:bold">*</span><span style="color:#000">GetMainWnd</span><span style="color:#000;font-weight:bold">();</span>
<span style="color:#204a87;font-weight:bold">void</span> <span style="color:#000">AddDocTemplate</span><span style="color:#000;font-weight:bold">(</span><span style="color:#000">CDocTemplate</span><span style="color:#ce5c00;font-weight:bold">*</span> <span style="color:#000">Template</span><span style="color:#000;font-weight:bold">);</span> <span style="color:#8f5902;font-style:italic">//把一个文档模板加入到CWinApp类的对象中
</span><span style="color:#8f5902;font-style:italic"></span><span style="color:#000">afx</span><span style="color:#a40000">\</span><span style="color:#000">_msg</span> <span style="color:#204a87;font-weight:bold">void</span> <span style="color:#000">OnFileNew</span><span style="color:#000;font-weight:bold">();</span> <span style="color:#8f5902;font-style:italic">//对 File|New 菜单消息的处理功能
</span><span style="color:#8f5902;font-style:italic"></span><span style="color:#000">afx</span><span style="color:#a40000">\</span><span style="color:#000">_msg</span> <span style="color:#204a87;font-weight:bold">void</span> <span style="color:#000">OnFileOpen</span><span style="color:#000;font-weight:bold">();</span> <span style="color:#8f5902;font-style:italic">//对 File|Open 处理
</span></code></pre></div></li>
</ul>
<h3 id="其它类">其它类<a href="#其它类" class="anchor" aria-hidden="true"><i class="iconfont icon-link"></i></a></h3>
<ul>
<li>
<p>绘图类</p>
<ul>
<li>CDC 绘图环境类：实现 Windows 应用程序中的绘图功能：文本以及几何图形（线、矩形、椭圆等）的输出
<ul>
<li>CClientDC: CDC 派生类，用于在一个窗口的客户区绘图</li>
<li>应用框架调用 CView 的 OnDraw 成员函数时，会自动创建一个 CDC 类的对象，作为参数传给 OnDraw</li>
</ul>
</li>
</ul>
<div class="highlight"><pre style="background-color:#f8f8f8;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-cpp" data-lang="cpp"><span style="color:#8f5902;font-style:italic">//文本输出函数
</span><span style="color:#8f5902;font-style:italic"></span><span style="color:#204a87;font-weight:bold">virtual</span> <span style="color:#000">BOOL</span> <span style="color:#000">TextOut</span><span style="color:#000;font-weight:bold">(</span><span style="color:#204a87;font-weight:bold">int</span> <span style="color:#000">x</span><span style="color:#000;font-weight:bold">,</span> <span style="color:#204a87;font-weight:bold">int</span> <span style="color:#000">y</span><span style="color:#000;font-weight:bold">,</span> <span style="color:#000">LPCTSTR</span> <span style="color:#000">lpszString</span><span style="color:#000;font-weight:bold">,</span> <span style="color:#204a87;font-weight:bold">int</span> <span style="color:#000">nCount</span><span style="color:#000;font-weight:bold">);</span>
<span style="color:#000">BOOL</span> <span style="color:#000">TextOut</span><span style="color:#000;font-weight:bold">(</span><span style="color:#204a87;font-weight:bold">int</span> <span style="color:#000">x</span><span style="color:#000;font-weight:bold">,</span> <span style="color:#204a87;font-weight:bold">int</span> <span style="color:#000">y</span><span style="color:#000;font-weight:bold">,</span> <span style="color:#204a87;font-weight:bold">const</span> <span style="color:#000">CString</span><span style="color:#ce5c00;font-weight:bold">&amp;</span> <span style="color:#000">str</span><span style="color:#000;font-weight:bold">);</span>
<span style="color:#204a87;font-weight:bold">virtual</span> <span style="color:#204a87;font-weight:bold">int</span> <span style="color:#000">DrawText</span><span style="color:#000;font-weight:bold">(</span><span style="color:#000">LPCTSTR</span> <span style="color:#000">lpszString</span><span style="color:#000;font-weight:bold">,</span> <span style="color:#204a87;font-weight:bold">int</span> <span style="color:#000">nCount</span><span style="color:#000;font-weight:bold">,</span> <span style="color:#000">LPRECT</span> <span style="color:#000">lpRect</span><span style="color:#000;font-weight:bold">,</span> <span style="color:#000">UINT</span> <span style="color:#000">nFormat</span><span style="color:#000;font-weight:bold">);</span>
<span style="color:#204a87;font-weight:bold">int</span> <span style="color:#000">DrawText</span><span style="color:#000;font-weight:bold">(</span><span style="color:#204a87;font-weight:bold">const</span> <span style="color:#000">CString</span><span style="color:#ce5c00;font-weight:bold">&amp;</span> <span style="color:#000">str</span><span style="color:#000;font-weight:bold">,</span> <span style="color:#000">LPRECT</span> <span style="color:#000">lpRect</span><span style="color:#000;font-weight:bold">,</span> <span style="color:#000">UINT</span> <span style="color:#000">nFormat</span><span style="color:#000;font-weight:bold">);</span>
<span style="color:#204a87;font-weight:bold">virtual</span> <span style="color:#000">COLORREF</span> <span style="color:#000">SetTextColor</span><span style="color:#000;font-weight:bold">(</span><span style="color:#000">COLORREF</span> <span style="color:#000">crColor</span> <span style="color:#000;font-weight:bold">);</span> <span style="color:#8f5902;font-style:italic">//设置字符和背景颜色
</span><span style="color:#8f5902;font-style:italic"></span><span style="color:#204a87;font-weight:bold">virtual</span> <span style="color:#000">COLORREF</span> <span style="color:#000">SetBkColor</span><span style="color:#000;font-weight:bold">(</span><span style="color:#000">COLORREF</span> <span style="color:#000">crColor</span><span style="color:#000;font-weight:bold">);</span>
<span style="color:#8f5902;font-style:italic">//几何图形输出函数
</span><span style="color:#8f5902;font-style:italic">//线段
</span><span style="color:#8f5902;font-style:italic"></span><span style="color:#000">CPoint</span> <span style="color:#000">MoveTo</span><span style="color:#000;font-weight:bold">(</span><span style="color:#204a87;font-weight:bold">int</span> <span style="color:#000">x</span><span style="color:#000;font-weight:bold">,</span> <span style="color:#204a87;font-weight:bold">int</span> <span style="color:#000">y</span><span style="color:#000;font-weight:bold">);</span><span style="color:#8f5902;font-style:italic">//定起点
</span><span style="color:#8f5902;font-style:italic"></span><span style="color:#000">CPoint</span> <span style="color:#000">MoveTo</span><span style="color:#000;font-weight:bold">(</span><span style="color:#000">POINT</span> <span style="color:#000">point</span><span style="color:#000;font-weight:bold">);</span>
<span style="color:#000">BOOL</span> <span style="color:#000">LineTo</span><span style="color:#000;font-weight:bold">(</span><span style="color:#000">POINT</span> <span style="color:#000">point</span><span style="color:#000;font-weight:bold">);</span>
<span style="color:#000">BOOL</span> <span style="color:#000">LineTo</span><span style="color:#000;font-weight:bold">(</span><span style="color:#204a87;font-weight:bold">int</span> <span style="color:#000">x</span><span style="color:#000;font-weight:bold">,</span> <span style="color:#204a87;font-weight:bold">int</span> <span style="color:#000">y</span><span style="color:#000;font-weight:bold">);</span>
<span style="color:#8f5902;font-style:italic">//画矩形
</span><span style="color:#8f5902;font-style:italic"></span><span style="color:#000">BOOL</span> <span style="color:#000">Rectangle</span><span style="color:#000;font-weight:bold">(</span><span style="color:#204a87;font-weight:bold">int</span> <span style="color:#000">x1</span><span style="color:#000;font-weight:bold">,</span> <span style="color:#204a87;font-weight:bold">int</span> <span style="color:#000">y1</span><span style="color:#000;font-weight:bold">,</span> <span style="color:#204a87;font-weight:bold">int</span> <span style="color:#000">x2</span><span style="color:#000;font-weight:bold">,</span> <span style="color:#204a87;font-weight:bold">int</span> <span style="color:#000">y2</span><span style="color:#000;font-weight:bold">);</span>
<span style="color:#000">BOOL</span> <span style="color:#000">Rectangle</span><span style="color:#000;font-weight:bold">(</span><span style="color:#000">LPCRECT</span> <span style="color:#000">lpRect</span><span style="color:#000;font-weight:bold">);</span>
<span style="color:#8f5902;font-style:italic">//画椭圆，参数为外接矩形
</span><span style="color:#8f5902;font-style:italic"></span><span style="color:#000">BOOL</span> <span style="color:#000">Ellipse</span><span style="color:#000;font-weight:bold">(</span><span style="color:#204a87;font-weight:bold">int</span> <span style="color:#000">x1</span><span style="color:#000;font-weight:bold">,</span> <span style="color:#204a87;font-weight:bold">int</span> <span style="color:#000">y1</span><span style="color:#000;font-weight:bold">,</span> <span style="color:#204a87;font-weight:bold">int</span> <span style="color:#000">x2</span><span style="color:#000;font-weight:bold">,</span> <span style="color:#204a87;font-weight:bold">int</span> <span style="color:#000">y2</span><span style="color:#000;font-weight:bold">);</span>
<span style="color:#000">BOOL</span> <span style="color:#000">Ellipse</span><span style="color:#000;font-weight:bold">(</span><span style="color:#000">LPCRECT</span> <span style="color:#000">lpRect</span><span style="color:#000;font-weight:bold">);</span>
<span style="color:#8f5902;font-style:italic">//绘图工具选择函数
</span><span style="color:#8f5902;font-style:italic"></span><span style="color:#204a87;font-weight:bold">virtual</span> <span style="color:#000">CGdiObject</span><span style="color:#ce5c00;font-weight:bold">*</span> <span style="color:#000">SelectStockObject</span><span style="color:#000;font-weight:bold">(</span><span style="color:#204a87;font-weight:bold">int</span> <span style="color:#000">nIndex</span><span style="color:#000;font-weight:bold">);</span> <span style="color:#8f5902;font-style:italic">//选择系统提供的绘图工具
</span><span style="color:#8f5902;font-style:italic"></span><span style="color:#000">CPen</span><span style="color:#ce5c00;font-weight:bold">*</span> <span style="color:#000">SelectObject</span><span style="color:#000;font-weight:bold">(</span><span style="color:#000">CPen</span><span style="color:#ce5c00;font-weight:bold">*</span> <span style="color:#000">pPen</span><span style="color:#000;font-weight:bold">);</span> <span style="color:#8f5902;font-style:italic">//选择自定义的笔
</span><span style="color:#8f5902;font-style:italic"></span><span style="color:#000">CBrush</span><span style="color:#ce5c00;font-weight:bold">*</span> <span style="color:#000">SelectObject</span><span style="color:#000;font-weight:bold">(</span><span style="color:#000">CBrush</span><span style="color:#ce5c00;font-weight:bold">*</span> <span style="color:#000">pBrush</span><span style="color:#000;font-weight:bold">);</span>
<span style="color:#204a87;font-weight:bold">virtual</span> <span style="color:#000">CFont</span><span style="color:#ce5c00;font-weight:bold">*</span> <span style="color:#000">SelectObject</span><span style="color:#000;font-weight:bold">(</span><span style="color:#000">CFont</span><span style="color:#ce5c00;font-weight:bold">*</span> <span style="color:#000">pFont</span><span style="color:#000;font-weight:bold">);</span>
</code></pre></div><ul>
<li>
<p>绘图工具类</p>
<ul>
<li>CPen: 画笔，用于画线</li>
<li>CFont: 字体</li>
<li>CBrush：刷子，用于填充封闭图形</li>
</ul>
<div class="highlight"><pre style="background-color:#f8f8f8;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-cpp" data-lang="cpp"><span style="color:#000">Cpen</span><span style="color:#000;font-weight:bold">(</span><span style="color:#204a87;font-weight:bold">int</span> <span style="color:#000">nPenStyle</span><span style="color:#000;font-weight:bold">,</span> <span style="color:#204a87;font-weight:bold">int</span> <span style="color:#000">nWidth</span><span style="color:#000;font-weight:bold">,</span> <span style="color:#000">COLORREF</span> <span style="color:#000">crColor</span><span style="color:#000;font-weight:bold">);</span>
<span style="color:#000">CBrush</span><span style="color:#000;font-weight:bold">(</span><span style="color:#000">COLORREF</span> <span style="color:#000">crColor</span><span style="color:#000;font-weight:bold">);</span>
<span style="color:#000">CFont</span><span style="color:#000;font-weight:bold">();</span>
<span style="color:#000">BOOL</span> <span style="color:#000">CFont</span><span style="color:#ce5c00;font-weight:bold">::</span><span style="color:#000">CreatePointFont</span><span style="color:#000;font-weight:bold">(</span><span style="color:#204a87;font-weight:bold">int</span> <span style="color:#000">nPointSize</span><span style="color:#000;font-weight:bold">,</span> <span style="color:#000">LPCTSTR</span> <span style="color:#000">lpszFaceName</span><span style="color:#000;font-weight:bold">,</span> <span style="color:#000">CDC</span><span style="color:#ce5c00;font-weight:bold">*</span> <span style="color:#000">pDC</span> <span style="color:#ce5c00;font-weight:bold">=</span> <span style="color:#204a87">NULL</span><span style="color:#000;font-weight:bold">);</span>
</code></pre></div></li>
</ul>
</li>
</ul>
<div class="highlight"><pre style="background-color:#f8f8f8;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-cpp" data-lang="cpp"><span style="color:#8f5902;font-style:italic">//绘图实例
</span><span style="color:#8f5902;font-style:italic"></span><span style="color:#204a87;font-weight:bold">void</span> <span style="color:#000">CMyView</span><span style="color:#ce5c00;font-weight:bold">::</span><span style="color:#000">OnDraw</span><span style="color:#000;font-weight:bold">(</span><span style="color:#000">CDC</span><span style="color:#ce5c00;font-weight:bold">*</span> <span style="color:#000">pDC</span><span style="color:#000;font-weight:bold">){</span>
  <span style="color:#000">COLORREF</span> <span style="color:#000">old</span><span style="color:#a40000">\</span><span style="color:#000">_text</span><span style="color:#a40000">\</span><span style="color:#000">_color</span><span style="color:#ce5c00;font-weight:bold">=</span><span style="color:#000">pDC</span><span style="color:#ce5c00;font-weight:bold">-&gt;</span><span style="color:#000">SetTextColor</span><span style="color:#000;font-weight:bold">(</span><span style="color:#000">RGB</span><span style="color:#000;font-weight:bold">(</span><span style="color:#0000cf;font-weight:bold">255</span><span style="color:#000;font-weight:bold">,</span><span style="color:#0000cf;font-weight:bold">0</span><span style="color:#000;font-weight:bold">,</span><span style="color:#0000cf;font-weight:bold">0</span><span style="color:#000;font-weight:bold">));</span> <span style="color:#8f5902;font-style:italic">//把字符颜色设置成“红”色。
</span><span style="color:#8f5902;font-style:italic"></span>  <span style="color:#000">COLORREF</span> <span style="color:#000">old</span><span style="color:#a40000">\</span><span style="color:#000">_bk</span><span style="color:#a40000">\</span><span style="color:#000">_color</span><span style="color:#ce5c00;font-weight:bold">=</span><span style="color:#000">pDC</span><span style="color:#ce5c00;font-weight:bold">-&gt;</span><span style="color:#000">SetBkColor</span><span style="color:#000;font-weight:bold">(</span><span style="color:#000">RGB</span><span style="color:#000;font-weight:bold">(</span><span style="color:#0000cf;font-weight:bold">0</span><span style="color:#000;font-weight:bold">,</span><span style="color:#0000cf;font-weight:bold">255</span><span style="color:#000;font-weight:bold">,</span><span style="color:#0000cf;font-weight:bold">0</span><span style="color:#000;font-weight:bold">));</span> <span style="color:#8f5902;font-style:italic">//把字符背景颜色设置成“绿”色。
</span><span style="color:#8f5902;font-style:italic"></span>  <span style="color:#000">pDC</span><span style="color:#ce5c00;font-weight:bold">-&gt;</span><span style="color:#000">TextOut</span><span style="color:#000;font-weight:bold">(</span><span style="color:#0000cf;font-weight:bold">0</span><span style="color:#000;font-weight:bold">,</span><span style="color:#0000cf;font-weight:bold">0</span><span style="color:#000;font-weight:bold">,</span><span style="color:#a40000">”</span><span style="color:#000">hello</span><span style="color:#a40000">”</span><span style="color:#000;font-weight:bold">);</span> <span style="color:#8f5902;font-style:italic">//在位置(0,0)处显示字符串&#34;hello&#34;
</span><span style="color:#8f5902;font-style:italic"></span>  <span style="color:#000">pDC</span><span style="color:#ce5c00;font-weight:bold">-&gt;</span><span style="color:#000">SetTextColor</span><span style="color:#000;font-weight:bold">(</span><span style="color:#000">old</span><span style="color:#a40000">\</span><span style="color:#000">_text</span><span style="color:#a40000">\</span><span style="color:#000">_color</span><span style="color:#000;font-weight:bold">);</span> <span style="color:#8f5902;font-style:italic">//把原来的字符颜色选回CDC类的对象
</span><span style="color:#8f5902;font-style:italic"></span>  <span style="color:#000">pDC</span><span style="color:#ce5c00;font-weight:bold">-&gt;</span><span style="color:#000">SetBkColor</span><span style="color:#000;font-weight:bold">(</span><span style="color:#000">old</span><span style="color:#a40000">\</span><span style="color:#000">_bk</span><span style="color:#a40000">\</span><span style="color:#000">_color</span><span style="color:#000;font-weight:bold">);</span> <span style="color:#8f5902;font-style:italic">//把原来的字符背景颜色选回CDC类的对象
</span><span style="color:#8f5902;font-style:italic"></span>  <span style="color:#000">CBrush</span> <span style="color:#000">brush</span><span style="color:#000;font-weight:bold">(</span><span style="color:#000">RGB</span><span style="color:#000;font-weight:bold">(</span><span style="color:#0000cf;font-weight:bold">0</span><span style="color:#000;font-weight:bold">,</span><span style="color:#0000cf;font-weight:bold">0</span><span style="color:#000;font-weight:bold">,</span><span style="color:#0000cf;font-weight:bold">255</span><span style="color:#000;font-weight:bold">)),</span><span style="color:#ce5c00;font-weight:bold">*</span><span style="color:#000">old</span><span style="color:#a40000">\</span><span style="color:#000">_brush</span><span style="color:#000;font-weight:bold">;</span> <span style="color:#8f5902;font-style:italic">//创建一个蓝色的刷子
</span><span style="color:#8f5902;font-style:italic"></span>  <span style="color:#000">old</span><span style="color:#a40000">\</span><span style="color:#000">_brush</span> <span style="color:#ce5c00;font-weight:bold">=</span> <span style="color:#000">pDC</span><span style="color:#ce5c00;font-weight:bold">-&gt;</span><span style="color:#000">SelectObject</span><span style="color:#000;font-weight:bold">(</span><span style="color:#ce5c00;font-weight:bold">&amp;</span><span style="color:#000">brush</span><span style="color:#000;font-weight:bold">);</span> <span style="color:#8f5902;font-style:italic">//把新刷子选进CDC类的对象，原来的刷子由old\_brush指向
</span><span style="color:#8f5902;font-style:italic"></span>  <span style="color:#000">pDC</span><span style="color:#ce5c00;font-weight:bold">-&gt;</span><span style="color:#000">Rectangle</span><span style="color:#000;font-weight:bold">(</span><span style="color:#0000cf;font-weight:bold">0</span><span style="color:#000;font-weight:bold">,</span><span style="color:#0000cf;font-weight:bold">50</span><span style="color:#000;font-weight:bold">,</span><span style="color:#0000cf;font-weight:bold">100</span><span style="color:#000;font-weight:bold">,</span><span style="color:#0000cf;font-weight:bold">150</span><span style="color:#000;font-weight:bold">);</span> <span style="color:#8f5902;font-style:italic">//画一个内部为蓝色的矩形，左上角：(0,50)；右下角：(100,150)
</span><span style="color:#8f5902;font-style:italic"></span>  <span style="color:#000">pDC</span><span style="color:#ce5c00;font-weight:bold">-&gt;</span><span style="color:#000">SelectObject</span><span style="color:#000;font-weight:bold">(</span><span style="color:#000">old</span><span style="color:#a40000">\</span><span style="color:#000">_brush</span><span style="color:#000;font-weight:bold">);</span> <span style="color:#8f5902;font-style:italic">//把原来的刷子选回到CDC类的对象中，必须要做！
</span><span style="color:#8f5902;font-style:italic"></span><span style="color:#000;font-weight:bold">}</span>
</code></pre></div><ul>
<li>
<p>文件输入输出类</p>
<ul>
<li>CFile: 基于字节流的文件 I/O</li>
<li>CArchive: 通过重载操作符 &lt;&lt; &gt;&gt; 实现了对基本数据类型和 MFC 类对象的文件 I/O</li>
</ul>
<div class="highlight"><pre style="background-color:#f8f8f8;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-cpp" data-lang="cpp"><span style="color:#000">BOOL</span> <span style="color:#000">IsLoading</span><span style="color:#000;font-weight:bold">()</span> <span style="color:#204a87;font-weight:bold">const</span><span style="color:#000;font-weight:bold">;</span>
<span style="color:#000">BOOL</span> <span style="color:#000">IsStoring</span><span style="color:#000;font-weight:bold">()</span> <span style="color:#204a87;font-weight:bold">const</span><span style="color:#000;font-weight:bold">;</span>
</code></pre></div></li>
<li>
<p>常用数据类型类</p>
<ul>
<li>CPoint（点坐标）</li>
<li>CRect（矩形信息）</li>
<li>CSize（矩形的宽度/高度、点之间的偏移量等）</li>
<li>CString</li>
</ul>
</li>
</ul>

						</div>
					</main>
					<div class="d-none d-lg-block col-lg-3 bd-toc d-print-none">
						<div class="btn-group-vertical w-100 my-3">
    
    <a class="btn btn-outline-secondary text-dark w-100 p-2" href="https://list.html" target="_blank">
        <i class="iconfont icon-LC_icon_list_line"></i><br />待更新列表
    </a>
    

    
    <a class="btn btn-outline-secondary text-dark w-100 p-2" href="mailto:littlegreedy@qq.com"
        target="_blank">
        <i class="iconfont icon-discussion"></i><br />纠错与咨询
    </a>
    
    
    
    
        <a class="btn btn-outline-secondary text-dark w-100 p-2" href="#" onclick="window.print()">
            <i class="iconfont icon-dayin"></i><br />打印本页
        </a>
    
    
</div>
						<h4 class="card-title pb-0">目录</h4>
						<nav id="TableOfContents">
  <ul>
    <li><a href="#windows-简介">Windows 简介</a></li>
    <li><a href="#消息驱动程序结构">消息驱动程序结构</a></li>
    <li><a href="#windows-api">Windows API</a></li>
    <li><a href="#mfc">MFC</a>
      <ul>
        <li><a href="#窗口类">窗口类</a></li>
        <li><a href="#文档类">文档类</a></li>
        <li><a href="#应用程序类">应用程序类</a></li>
        <li><a href="#其它类">其它类</a></li>
      </ul>
    </li>
  </ul>
</nav>
						
						
						<div id="disqus_thread"></div>
							<script>
							

							

							(function() { 
							var d = document, s = d.createElement('script');
							s.src = "https://"+"your site name on disqus"+".disqus.com/embed.js";
							s.setAttribute('data-timestamp', +new Date());
							(d.head || d.body).appendChild(s);
							})();
							</script>
							<noscript>Please enable JavaScript to view the <a href="https://disqus.com/?ref_noscript">comments powered by Disqus.</a></noscript>                  
					  	</div>
						
					</div>
				</div>
			</div>
		</div>
	</div>

	<script
    src="https://code.jquery.com/jquery-3.4.1.min.js"
    integrity="sha256-CSXorXvZcTkaix6Yvo6HppcZGetbYMGWSFlBw8HfCJo="
    crossorigin="anonymous"></script><script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.7/umd/popper.min.js" integrity="sha384-UO2eT0CpHqdSJQ6hJty5KVphtPhzWj9WO1clHTMGa3JDZwrnQq4sF86dIHNDz0W1" crossorigin="anonymous"></script>
    <script src="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js" integrity="sha384-JjSmVgyd0p3pXB1rRibZUAYoIIy6OrQ6VrjIEaFf/nJGzIxFDsf4x0xIM+B07jRM" crossorigin="anonymous"></script><script defer src="https://cdn.jsdelivr.net/npm/katex@0.11.1/dist/katex.min.js" integrity="sha384-y23I5Q6l+B6vatafAwxRu/0oK/79VlbSz7Q9aiSZUvyWYIYsd+qj+o24G5ZU2zJz" crossorigin="anonymous"></script>
<script defer src="https://cdn.jsdelivr.net/npm/katex@0.11.1/dist/contrib/auto-render.min.js" integrity="sha384-kWPLUVMOks5AQFrykwIup5lo0m3iMkkHrD0uJ4H5cjeGihAutqP0yW0J6dpFiVkI" crossorigin="anonymous"
onload="renderMathInElement(document.body);"></script>

<script type="text/javascript" src="https://fziks.gitee.io/custom.min.d3e1b7647f32dbe7e0140398739a26dad3f3470fc1eebe0741ef33668f1b7bd0b2917dc6efb9f0d9f1092b91dca502cab1b883863f02530133a8a8ef609926af.js" integrity="sha512-0&#43;G3ZH8y2&#43;fgFAOYc5om2tPzRw/B7r4HQe8zZo8be9CykX3G77nw2fEJK5HcpQLKsbiDhj8CUwEzqKjvYJkmrw=="></script>
<script type="text/javascript">

document.addEventListener("DOMContentLoaded", function () {
    renderMathInElement(
        document.body, {
            delimiters: [
                {
                    left: "$$",
                    right: "$$",
                    display: true
                },
                {
                    left: "\\[",
                    right: "\\]",
                    display: true
                },
                {
                    left: "$",
                    right: "$",
                    display: false
                },
                {
                    left: "\\(",
                    right: "\\)",
                    display: false
                }
            ],
            strict: false
        }
    );
});


$(document).on('click', 'a[href^="#"]', function (event) {
    event.preventDefault();

    $('html, body').animate({
        scrollTop: $($.attr(this, 'href')).offset().top
    }, 500);
});
</script>




</body>

</html>